######################################
# Generate plots conjoint data
######################################

rm(list=ls())

library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

# set working directory
setwd("~/Dropbox/Cerrillos 2017/08_replication")

####################
# left 
####################

# load data
d <- read.table("028_left_no30.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:"
),order=c(.5,2.1,2.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Left-wing respondents") + theme(plot.title = element_text(size=25, hjust = 0.5))
f_left

####################
# right
####################

# load data
d <- read.table("029_right_no30.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:"
),order=c(.5,2.1,2.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Right-wing respondents") + theme(plot.title = element_text(size=25, hjust = 0.5))
f_right

####################
# center
####################

# load data
d <- read.table("030_center_no30.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"

  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:"
),order=c(.5,2.1,2.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Centrist respondents") + theme(plot.title = element_text(size=25, hjust = 0.5)) 
f_center

####################
# no ideology
####################

# load data
d <- read.table("031_noideo_no30.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"

  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:"
),order=c(.5,2.1,2.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Non-identifiers") + theme(plot.title = element_text(size=25, hjust = 0.5)) 
f_noideo

#############################
# Appendix L
#############################

pdf('~/Dropbox/Cerrillos 2017/08_replication/f_main_no30.pdf',width=15,height=15)
ggarrange(f_left,f_right,f_center,f_noideo, nrow = 2, ncol = 2)
dev.off()


